// pages/invContdetails/invContdetails.js
import api from '../../utils/api';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    coId: '',
    unitId: '',
    id: '', //页面传
    showType: false, //展示下拉选择
    columns: [], //下拉选
    fpCode: '',
    deptName: '',
    userName: '',
    typeValue: '',
    typeValueCode: '',
    model: '',
    production: '',
    unit: '',
    num: '',
    price: '',
    money: '',
    saveButton: '', //是否显示保存按钮（是否能显示）

  },
  //去选择
  goselect(e) {
    console.log(e, '去选择')
    wx.navigateTo({
      url: '/pages/categorySelection/categorySelection?index= ' + e.currentTarget.dataset.index,
    })
  },
  focus(e) {
    console.log(e.detail.value, "focus")
    this.setData({
      typeIndex: e.currentTarget.dataset.index, //获取下边
      showType: true,
      typeValue: e.detail.value
    });

    this.showList()

  },
  change(e) {
    console.log(e.detail, "change")
    this.setData({
      showType: true,
      typeValue: e.detail
    });
    this.showList()
  },
  iconclick(e) {
    this.setData({
      typeValue: ''
    })
    this.showList()

  },
  // 开票内容详情--货物或应税劳务，服务名称 列表
  showList() {
    let that = this;
    api.taskList({
      data: {
        coId: this.data.coId,
        word: this.data.typeValue,
      },
      success: function (res) {
        console.log(res.data.data, "taskList")
        if (res.data.code = 200) {
          that.data.columns = [];
          res.data.data.forEach((item) => {
            let obj = {
              text: item.name,
              code: item.code,
              price: item.price,
              unit: item.unit,
              model: item.model,
            }
            that.data.columns.push(obj)
            console.log(that.data.columns, 'oooo')
          });
          that.setData({
            columns: that.data.columns,
          })
          that.setData({
            showType: true
          })
        } else {
          that.setData({
            showType: false
          })
        }
      }
    })
  },
  //点击遮罩层取消
  overlay() {
    this.onCancelType()
  },
  onCancelType() {
    this.setData({
      typeValueCode: '',
      typeValue: '',
      showType: false
    });
  },
  onConfirmType(event) {
    console.log(event.detail)
    const {
      value
    } = event.detail;
    console.log(value, "val")
    let text = value.text;
    // this.data.price = value.price;
    // this.data.unit = value.unit;
    // this.data.model = value.model;

    this.setData({
      typeValue: value.text,
      typeValueCode: value.code,
      price: value.price,
      model: value.model,
      unit: value.unit,
      showType: false
    })
  },
  onmodelChange(e) {
    this.setData({
      model: e.detail
    })
  },
  onproductionChange(e) {
    this.setData({
      production: e.detail
    })
  },
  onunitChange(e) {
    // console.log(e.detail,'单位')
    this.setData({
      unit: e.detail
    })
  },

  //自动计算金额
  onnumChange(e) {
    // console.log(e.detail,'数量')
    this.setData({
      num: e.detail
    })
  },
  //自动计算金额
  onpriceChange(e) {
    // console.log(e.detail,'单价')
    this.setData({
      price: e.detail
    })
  },
  onmoneyChange(e) {
    if (this.data.num && this.data.price) {
      this.setData({
        money: this.data.num * this.data.price
      })
    } else {
     this.setData({
        money: e.detail
      })
    }
  },
  ongroupNochange(e) {
    console.log(e.detail)
    this.setData({
      groupNo: e.detail
    })
  },

  //保存
  save() {
    if (!this.data.production || !this.data.money) {
      if (!this.data.production) {
        wx.showToast({
          title: '请填写产品名称',
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '金额不能为空',
          icon: 'none'
        })

      }
    } else {
      this.baocun()
    }
  },
  baocun() {
    let that = this;
    api.invcontentsSave({
      method: 'post',
      data: {
        id: this.data.id,
        content: this.data.typeValue,
        contentCode: this.data.typeValueCode,
        model: this.data.model,
        production: this.data.production,
        unit: this.data.unit,
        num: this.data.num,
        price: this.data.price,
        money: this.data.money,
        groupNo: this.data.groupNo
      },
      success: function (res) {
        console.log(res.data.data, "invcontentsSave")
        if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'success'
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 100);

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id: options.id,
      saveButton: options.saveButton
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.coId = wx.getStorageSync('userInfo').coId
    this.data.unitId = wx.getStorageSync('userInfo').unitId

    let that = this;
    api.invcontentsDetails({
      data: {
        id: this.data.id
      },
      success: function (res) {
        console.log(this.data.id,res, "idddd")
        // console.log(res.data.data, "invcontentsDetails")
        if (res.data.code == 200) {
          that.setData({
            fpCode: res.data.data.fpCode,
            userName: res.data.data.userName,
            deptName: res.data.data.deptName,
            typeValue: res.data.data.content,
            typeValueCode: res.data.data.contentCode,
            unit: res.data.data.unit,
            model: res.data.data.model,
            production: res.data.data.production,
            num: res.data.data.num,
            price: res.data.data.price,
            money: res.data.data.money,
            groupNo: res.data.data.groupNo
          })

          // wx.navigateBack({//返回 携带的参数
          let pages = getCurrentPages();
          let currPage = pages[pages.length - 1];
          console.log(that.data.content, that.data.contentCode)
          if (that.data.content && that.data.contentCode) {
            console.log('okk')
            that.setData({
              typeValue: that.data.content,
              typeValueCode: that.data.contentCode,
            })
            console.log(that.data.typeValueCode, that.data.typeValueCode)
          }
        }
        console.log(that.data.typeValueCode, 'code')
      }
    })


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})